package com.mrjy.payment.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.VipPackageInfo;
import com.mrjy.payment.dao.IVipPackageInfoDao;
import com.mrjy.payment.dto.VipPackageInfoDTO;

@Service("VipPackageInfoBo")
public class VipPackageInfoBo implements IVipPackageInfoBo{
	
	@Resource
	private IVipPackageInfoDao vipPackageInfoDao;
	

	@Override
	public Object txInsert(VipPackageInfo info) {
		Map<String , Object> map = new HashMap<String, Object>();
		
		if(StringUtils.isBlank(info.getTypeName())){
			map.put("err_code", 101);
			map.put("err_msg", "套餐类型不能为空");
			return map;
		}
		if(info.getPrice() == null){
			map.put("err_code", 102);
			map.put("err_msg", "原价格不能为空");
			return map;
		}
		if(info.getDiscountPrice() == null){
			map.put("err_code", 103);
			map.put("err_msg", "折扣价不能为空");
			return map;
		}
		info.setCreateTime(new Timestamp(System.currentTimeMillis()));
		info.setDisableFlag(0);
		//默认为0
		if(info.getVipDefault() == null){
			info.setVipDefault(0);
		}
		
		int count = vipPackageInfoDao.insert(info);
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败")	;
		}
		return map ;
		
	}
	@Override
	public Object queryById(int id) {
		Map<String, Object> map = new HashMap<String,Object>();
		VipPackageInfo info = vipPackageInfoDao.queryById(id);
		if(info != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", info);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}
	@Override
	public Object txUpdateById(VipPackageInfo info) {
		Map<String , Object> map = new HashMap<String, Object>();
		int count = vipPackageInfoDao.updateById(info);
		
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败")	;
		}
		return map ;
	}
	@Override
	public int deleteByIds(int[] ids) {
		try{
			return vipPackageInfoDao.deleteByIds(ids);
		}catch(DataAccessException  e){
			return 0;
		}
	}
	@Override
	public int updateDisableFlag(VipPackageInfo info) {
		return vipPackageInfoDao.updateDisableFlag(info);
	}
	@Override
	public Object queryListForBack(VipPackageInfo info) {
		Map<String , Object> map = new HashMap<String, Object>();
		int total = vipPackageInfoDao.queryCountForBack();
		if(total > 0){
			List<VipPackageInfo> list = vipPackageInfoDao.queryListForBack(info) ;
			if(list != null && list.size() > 0){
				map.put("err_code", 200);
				map.put("err_msg", "查询成功，有数据");
				map.put("list", list);
				map.put("total", total);
			}else{
				map.put("err_code", 100);
				map.put("err_msg", "无数据");
				map.put("list", null);
				map.put("total", total);
			}
		}else{
			map.put("err_code", 100);
			map.put("err_msg", "无数据");
			map.put("list", null);
			map.put("total", 0);
		}
		
		return map;
	}
	@Override
	public Object queryList(VipPackageInfo vipPackageInfo) {
		Map<String , Object> map = new HashMap<String, Object>();
		List<VipPackageInfoDTO> list = vipPackageInfoDao.queryList(vipPackageInfo) ;
		if(list != null && list.size() > 0){
			map.put("err_code", 200);
			map.put("err_msg", "查询成功，有数据");
			map.put("list", list);
		}else{
			map.put("err_code", 100);
			map.put("err_msg", "无数据");
			map.put("list", null);
		}
		return map;
	}
	
	
}
